package com.sharedprint.system.user.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sharedprint.system.user.service.UserInfoService;
import com.sharedprint.util.Token;

/**
 * 用户管理的Controller
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api/userInfo")
public class UserInfoController {
	
	@Autowired
	private UserInfoService userInfoService;
	
	/**
	 * 用户登录
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	@ResponseBody
	public Map<String, Object> login(HttpServletRequest request) {
		String userName = request.getParameter("username");
		String passwd = request.getParameter("upwd");
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", "0");
		if (userName != null && !"".equals(userName.trim()) && passwd != null && !"".equals(passwd.trim())) {
			Map<String, Object> searchMap = new HashMap<String, Object>();
			searchMap.put("userName", userName);
			searchMap.put("passwd", passwd);
			List<Map<String, Object>> userList = userInfoService.findUserList(searchMap);
			if (userList!=null && userList.size()==1) {
				Map<String, Object> userMap = userList.get(0);
				String token = Token.getTokenString(request.getSession());
				resultMap.put("success", "1");
				resultMap.put("token", token);
				resultMap.put("userId", userMap.get("uid"));
				resultMap.put("trueName", userMap.get("truename"));
				resultMap.put("userName", userName);
			}
		}
		return resultMap;
	}
}
